package com.csk.zsk.compents.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csk.zsk.commons.action.AjaxActionSupport;
import com.csk.zsk.commons.utils.encode.JsonBinder;
import com.csk.zsk.commons.utils.reflection.ConvertUtils;
import com.csk.zsk.commons.utils.web.struts2.Struts2Utils;
import com.csk.zsk.compents.entity.Music;
import com.csk.zsk.compents.entity.MusicWeekly;
import com.csk.zsk.compents.entity.Playlist;
import com.csk.zsk.compents.entity.RankingList;
import com.csk.zsk.compents.entity.User;
import com.csk.zsk.compents.service.MusicService;
import com.csk.zsk.compents.service.MusicWeeklyService;
import com.csk.zsk.compents.service.PlaylistService;
import com.csk.zsk.compents.service.RankingListService;
import com.csk.zsk.compents.utils.Constants;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

@Controller
@Scope("prototype")
public class MusicAction extends AjaxActionSupport {

	private static final long serialVersionUID = -9185637132492436922L;
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();

	@Autowired
	private MusicService musicService;
	@Autowired
	private PlaylistService playlistService;
	@Autowired
	private RankingListService rankingListService;
	@Autowired
	private MusicWeeklyService musicWeeklyService;

	public void importMusicWeekly() throws Exception {
		String title = Struts2Utils.getParameter("title");
		String imageUrl = Struts2Utils.getParameter("imageUrl");
		String content = Struts2Utils.getParameter("content");
		MusicWeekly musicWeekly = new MusicWeekly();
		musicWeekly.setTitle(title);
		musicWeekly.setImageUrl(imageUrl);
		musicWeekly.setContent(content);
		musicWeeklyService.saveMusicWeekly(musicWeekly);

	}

	public void importRankingList() throws Exception {
		String listName = Struts2Utils.getParameter("listName");
		String imageUrl = Struts2Utils.getParameter("imageUrl");
		String listDesc = Struts2Utils.getParameter("listDesc");
		RankingList rankingList = new RankingList();
		rankingList.setListName(listName);
		rankingList.setImageUrl(imageUrl);
		rankingList.setListDesc(listDesc);
		rankingListService.saveRankingList(rankingList);

	}

	public void getDefaultMusiclist() throws Exception {
		User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
		String userId = user.getId();
		List<Music> musicList = null;
		Playlist playlist = playlistService.getLovePlaylistByUserId(userId);
		if (playlist == null) {
			RankingList rankingList = rankingListService.getRankingList("1");// id为1是糖果盒推荐榜的，数据库写死的
			musicList = rankingList.getMusicList();
		} else {
			if (playlist.getMusics() != null && !"".equals(playlist.getMusics())) {
				String[] musicIds = playlist.getMusics().split(",");
				List<String> musicIdList = Arrays.asList(musicIds);
				musicList = musicService.getMusicListByIds(musicIdList);
			} else {
				RankingList rankingList = rankingListService.getRankingList("1");// id为1是糖果盒推荐榜的，数据库写死的
				musicList = rankingList.getMusicList();
			}
		}
		musicList = Lists.newArrayList();
		Music m1 = new Music();
		Music m2 = new Music();
		m1.setId("1");
		m1.setSongName("1111");
		m1.setSingerName("1111111");
		m1.setMp3url("compents/music/440hz.mp3");

		m2.setId("2");
		m2.setSongName("22222222");
		m2.setSingerName("22222222");
		m2.setMp3url("compents/music/880hz.mp3");
		musicList.add(m2);
		musicList.add(m1);
		String json = binder.toJson(musicList);
		Struts2Utils.renderJson(json);
	}

	public void getMusicWeekly() throws Exception {
		Integer num = Integer.parseInt(Struts2Utils.getParameter("num"));
		List<MusicWeekly> musicWeeklyList = musicWeeklyService.getMusicWeeklyByNum(num);
		String json = binder.toJson(musicWeeklyList);
		Struts2Utils.renderJson(json);
	}

	public void getMusicWeeklyById() throws Exception {
		String musicWeeklyId = Struts2Utils.getParameter("musicWeeklyId");
		MusicWeekly musicWeekly = musicWeeklyService.getMusicWeeklyById(musicWeeklyId);
		String json = binder.toJson(musicWeekly);
		Struts2Utils.renderJson(json);
	}

	public void getRankingListById() throws Exception {
		String rankingListId = Struts2Utils.getParameter("rankingListId");
		RankingList rankingList = rankingListService.getRankingList(rankingListId);
		String json = binder.toJson(rankingList);
		Struts2Utils.renderJson(json);
	}

	public void getMusicByMusicWeeklyId() throws Exception {
		List<Music> musicList = Lists.newArrayList();
		String musicWeeklyId = Struts2Utils.getParameter("musicWeeklyId");
		MusicWeekly musicWeekly = musicWeeklyService.getMusicWeeklyById(musicWeeklyId);
		String musics = musicWeekly.getMusics();
		if (musics != null && !"".equals(musics)) {
			String[] musicIds = musics.split(",");
			List<String> musicIdList = Arrays.asList(musicIds);
			musicList = musicService.getMusicListByIds(musicIdList);
		}
		String json = binder.toJson(musicList);
		Struts2Utils.renderJson(json);
	}

	public void getRankingList() throws Exception {
		List<RankingList> rankingList = rankingListService.getAllRankingList();
		String json = binder.toJson(rankingList);
		Struts2Utils.renderJson(json);
	}

	public void getMusicById() throws Exception {
		String id = Struts2Utils.getParameter("id");
		Music music = musicService.getMusicById(id);
		String json = binder.toJson(music);
		Struts2Utils.renderJson(json);
	}

	public void getMusicByListId() throws Exception {
		String rankingListId = Struts2Utils.getParameter("rankingListId");
		RankingList rankingList = rankingListService.getRankingList(rankingListId);
		List<Music> musicList = rankingList.getMusicList();
		String json = binder.toJson(musicList);
		Struts2Utils.renderJson(json);
	}

	public void getMusicByPlaylistId() throws Exception {
		String playlistId = Struts2Utils.getParameter("playlistId");
		Playlist playlist = playlistService.getPlaylistById(playlistId);
		String[] musicIds = playlist.getMusics().split(",");
		List<String> musicIdList = Arrays.asList(musicIds);
		List<Music> musicList = musicService.getMusicListByIds(musicIdList);
		String json = binder.toJson(musicList);
		Struts2Utils.renderJson(json);
	}

	public void getNewMusic() throws Exception {
		int num = Integer.parseInt(Struts2Utils.getParameter("num"));
		List<Music> musicList = musicService.getNewMusic(num);
		String json = binder.toJson(musicList);
		Struts2Utils.renderJson(json);
	}

	public void getMusicByLike() throws Exception {
		String musicLike = Struts2Utils.getParameter("musicLike");
		if (musicLike == null || musicLike.equals("")) {
			return;
		}
		List<Music> musicList = musicService.searchMusicByLike(musicLike);
		String json = binder.toJson(musicList);
		Struts2Utils.renderJson(json);
	}

	public void savePlaylist() {
		try {
			Playlist playlist = null;
			User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
			String playlistId = Struts2Utils.getParameter("playlistId");
			if (playlistId != null && !"".equals(playlistId)) {
				playlist = playlistService.getPlaylistById(playlistId);
			} else {
				playlist = new Playlist();
			}
			playlist.setName(Struts2Utils.getParameter("name"));
			playlist.setDescp(Struts2Utils.getParameter("descp"));
			playlist.setImageUrl(Struts2Utils.getParameter("imageUrl"));
			playlist.setIsSys(false);
			playlist.setCreateTime(new Date());
			playlist.setUserId(user.getId());
			playlistService.savePlaylist(playlist);
			Struts2Utils.renderText("success");
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}
	}

	public void deletePlaylist() {
		try {
			String playlistId = Struts2Utils.getParameter("playlistId");
			playlistService.deletePlaylist(playlistId);
			Struts2Utils.renderText("success");
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}
	}

	public void getMyPlaylist() throws Exception {
		User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
		String userId = user.getId();
		List<Playlist> playlistList = playlistService.getPlaylistByUserId(userId);
		String playlistId = Struts2Utils.getParameter("playlistId");
		if (playlistId != null && !"".equals(playlistId)) {
			Playlist playlist = playlistService.getPlaylistById(playlistId);
			if (playlistList.contains(playlist)) {
				playlistList.remove(playlist);
			}
		}
		String json = binder.toJson(playlistList);
		Struts2Utils.renderJson(json);
	}

	public void getPlaylistById() throws Exception {
		String playlistId = Struts2Utils.getParameter("playlistId");
		Playlist playlist = playlistService.getPlaylistById(playlistId);
		String json = binder.toJson(playlist);
		Struts2Utils.renderJson(json);
	}

	public void addAllPlaylist() throws Exception {
		try {
			String playlistId = Struts2Utils.getParameter("playlistId");
			String objId = Struts2Utils.getParameter("objId");
			String type = Struts2Utils.getParameter("type");
			String newMusics = "";
			if (type.equals("1")) {
				MusicWeekly musicWeekly = musicWeeklyService.getMusicWeeklyById(objId);
				newMusics = musicWeekly.getMusics();
			} else if (type.equals("2")) {
				RankingList rankingList = rankingListService.getRankingList(objId);
				List<Music> musicList = rankingList.getMusicList();
				newMusics = ConvertUtils.convertElementPropertyToString(musicList, "id", ",");
			} else if (type.equals("3")) {
				Playlist pl = playlistService.getPlaylistById(objId);
				newMusics = pl.getMusics();
			}
			Playlist playlist = playlistService.getPlaylistById(playlistId);
			String oldMusic = (playlist.getMusics() == null ? "" : playlist.getMusics());
			String musics = MergeMusics(oldMusic, newMusics);
			playlist.setMusics(musics);
			playlistService.savePlaylist(playlist);
			Struts2Utils.renderText("success");
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}

	}

	public void addPlaylist() throws Exception {
		try {
			String playlistId = Struts2Utils.getParameter("playlistId");
			String newMusics = Struts2Utils.getParameter("musicId") + ",";
			Playlist playlist = playlistService.getPlaylistById(playlistId);
			String oldMusic = (playlist.getMusics() == null ? "" : playlist.getMusics());
			String musics = MergeMusics(oldMusic, newMusics);
			playlist.setMusics(musics);
			playlistService.savePlaylist(playlist);
			Struts2Utils.renderText("success");
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}

	}

	private String MergeMusics(String oldMusics, String newMusics) {
		StringBuilder sb = new StringBuilder("");
		Set<String> set = Sets.newHashSet();
		String[] oldMusicArray = oldMusics.split(",");
		String[] newMusicArray = newMusics.split(",");
		for (int i = 0; i < oldMusicArray.length; i++) {
			String oldMusic = oldMusicArray[i];
			if (oldMusic != null && !oldMusic.equals("")) {
				set.add(oldMusic);
			}
		}
		for (int j = 0; j < newMusicArray.length; j++) {
			String newMusic = newMusicArray[j];
			if (newMusic != null && !newMusic.equals("")) {
				set.add(newMusic);
			}
		}
		for (String s : set) {
			if (s != null && !"".equals(s)) {
				sb.append(s + ",");
			}
		}
		return sb.substring(0, sb.length() - 1);
	}
}
